home *** CD-ROM | disk | FTP | other *** search
/ PCGUIA 117 / PC Guia 117.iso / Software / Produtividade / Software2 / Product4 / Setup.exe / drupal-4.6.0 / includes / image.inc < prev    next >
Encoding:
Text File  |  2005-03-29  |  8.2 KB  |  300 lines

  1. <?php
  2. // $Id: image.inc,v 1.4 2005/03/29 00:01:23 unconed Exp $
  3.  
  4. /**
  5.  * Return a list of available toolkits.
  6.  *
  7.  * @return An array of toolkit name => descriptive title.
  8.  */
  9. function image_get_available_toolkits() {
  10.   $toolkits = file_scan_directory('includes', 'image\..*\.inc$');
  11.  
  12.   $output = array();
  13.   foreach ($toolkits as $file => $toolkit) {
  14.     include_once($file);
  15.     $function = str_replace('.', '_', $toolkit->name) .'_info';
  16.     if (function_exists($function)) {
  17.       $info = $function();
  18.       $output[$info['name']] = $info['title'];
  19.     }
  20.   }
  21.   $output['gd'] = t('Built-in GD2 toolkit');
  22.   return $output;
  23. }
  24.  
  25. /**
  26.  * Retrieve the name of the currently used toolkit.
  27.  *
  28.  * @return String containing the name of the toolkit.
  29.  */
  30. function image_get_toolkit() {
  31.   static $toolkit;
  32.   if (!$toolkit) {
  33.     $toolkit = variable_get('image_toolkit', 'gd');
  34.     $toolkit_file = 'includes/image.'.$toolkit.'.inc';
  35.     if ($toolkit != 'gd' && file_exists($toolkit_file)) {
  36.       include_once $toolkit_file;
  37.     }
  38.     elseif (!image_gd_check_settings()) {
  39.       $toolkit = false;
  40.     }
  41.   }
  42.  
  43.   return $toolkit;
  44. }
  45.  
  46. /**
  47.  * Invokes the given method using the currently selected toolkit.
  48.  *
  49.  * @param $method A string containing the method to invoke.
  50.  * @param $params An optional array of parameters to pass to the toolkit method.
  51.  *
  52.  * @return Mixed values (typically Boolean for successful operation).
  53.  */
  54. function image_toolkit_invoke($method, $params = array()) {
  55.   if ($toolkit = image_get_toolkit()) {
  56.     $function = 'image_'. $toolkit .'_'. $method;
  57.     if (function_exists($function)) {
  58.       return call_user_func_array($function, $params);
  59.     }
  60.     else {
  61.       watchdog('php', t("The selected image handling toolkit '%toolkit' can not correctly process '%function'.", array('%toolkit' => "<em>$toolkit</em>", '%function' => "<em>$function</em>")), WATCHDOG_ERROR);
  62.       return false;
  63.     }
  64.   }
  65.   else {
  66.     if ($method == 'settings') {
  67.       return image_gd_settings();
  68.     }
  69.   }
  70. }
  71.  
  72.  
  73. /**
  74.  * Get details about an image.
  75.  *
  76.  * @return array containing information about the image
  77.  *      'width': image's width in pixels
  78.  *      'height': image's height in pixels
  79.  *      'extension': commonly used extension for the image
  80.  *      'mime_type': image's MIME type ('image/jpeg', 'image/gif', etc.)
  81.  */
  82. function image_get_info($file) {
  83.   if (!file_exists($file)) {
  84.     return false;
  85.   }
  86.  
  87.   $details = false;
  88.   $data = @getimagesize($file);
  89.  
  90.   if (is_array($data)) {
  91.     $extensions = array('1' => 'gif', '2' => 'jpg', '3' => 'png');
  92.     $extension = array_key_exists($data[2], $extensions) ?  $extensions[$data[2]] : '';
  93.     $details = array('width'     => $data[0],
  94.                      'height'    => $data[1],
  95.                      'extension' => $extension,
  96.                      'mime_type' => $data['mime']);
  97.   }
  98.  
  99.   return $details;
  100. }
  101.  
  102. /**
  103.  * Scales an image to the given width and height while maintaining aspect
  104.  * ratio.
  105.  *
  106.  * @param $source         The filepath of the source image
  107.  * @param $destination    The file path of the destination image
  108.  * @param $width          The target width
  109.  * @param $height         The target height
  110.  *
  111.  * @return True or false, based on success
  112.  */
  113. function image_scale($source, $destination, $width, $height) {
  114.   $info = image_get_info($source);
  115.  
  116.   // don't scale up
  117.   if ($width > $info['width'] && $height > $info['height']) {
  118.     return false;
  119.   }
  120.  
  121.   $aspect = $info['height'] / $info['width'];
  122.   if ($aspect < $height / $width) {
  123.     $width = (int)min($width, $info['width']);
  124.     $height = (int)round($width * $aspect);
  125.   } else {
  126.     $height = (int)min($height, $info['height']);
  127.     $width = (int)round($height / $aspect);
  128.   }
  129.  
  130.   return image_toolkit_invoke('resize', array($source, $destination, $width, $height));
  131. }
  132.  
  133. /**
  134.  * Resize an image to the given dimensions (ignoring aspect ratio).
  135.  *
  136.  * @param $source        The filepath of the source image.
  137.  * @param $destination   The file path of the destination image.
  138.  * @param $width         The target width.
  139.  * @param $height        The target height.
  140.  */
  141. function image_resize($source, $destination, $width, $height) {
  142.   return image_toolkit_invoke('resize', array($source, $destination, $width, $height));
  143. }
  144.  
  145. /**
  146.  * Rotate an image by the given number of degrees.
  147.  *
  148.  * @param $source  The filepath of the source image
  149.  * @param $destination    The file path of the destination image
  150.  * @param $degrees The number of (clockwise) degrees to rotate the image
  151.  */
  152. function image_rotate($source, $destination, $degrees) {
  153.   return image_toolkit_invoke('rotate', array($source, $destination, $degrees));
  154. }
  155.  
  156. /**
  157.  * Crop an image to the rectangle specified by the given rectangle.
  158.  *
  159.  * @param $source        The filepath of the source image
  160.  * @param $destination   The file path of the destination image
  161.  * @param $x             The top left co-ordinate of the crop area (x axis value)
  162.  * @param $y             The top left co-ordinate of the crop area (y axis value)
  163.  * @param $width         The target width
  164.  * @param $height        The target height
  165.  */
  166. function image_crop($source, $destination, $x, $y, $width, $height) {
  167.   return image_toolkit_invoke('crop', array($source, $destination, $x, $y, $width, $height));
  168. }
  169.  
  170. /**
  171.  * GD2 toolkit functions
  172.  * With the minimal requirements of PHP 4.3 for Drupal, we use the built-in version of GD.
  173.  */
  174.  
  175. /**
  176.  * Retrieve settings for the GD2 toolkit (not used).
  177.  */
  178. function image_gd_settings() {
  179.   if (image_gd_check_settings()) {
  180.     return t('The built-in GD2 toolkit is installed and working properly.');
  181.   }
  182.   else {
  183.     form_set_error('image_toolkit', t("The built-in GD image toolkit requires that the GD module for PHP be installed and configured properly. For more information see %url.", array('%url' => '<a href="http://php.net/image">http://php.net/image</a>')));
  184.     return false;
  185.   }
  186. }
  187.  
  188. /**
  189.  * Verify GD2 settings (that the right version is actually installed).
  190.  *
  191.  * @return boolean
  192.  */
  193. function image_gd_check_settings() {
  194.   if ($check = get_extension_funcs('gd')) {
  195.     if (in_array('imagegd2', $check)) {
  196.       // GD2 support is available.
  197.       return true;
  198.     }
  199.   }
  200.   return false;
  201. }
  202.  
  203. /**
  204.  * Scale an image to the specified size using GD.
  205.  */
  206. function image_gd_resize($source, $destination, $width, $height) {
  207.   if (!file_exists($source)) {
  208.     return false;
  209.   }
  210.  
  211.   $info = image_get_info($source);
  212.   if (!$info) {
  213.     return false;
  214.   }
  215.  
  216.   $im = image_gd_open($source, $info['extension']);
  217.   if (!$im) {
  218.     return false;
  219.   }
  220.  
  221.   $res = imageCreateTrueColor($width, $height);
  222.   imageCopyResampled($res, $im, 0, 0, 0, 0, $width, $height, $info['width'], $info['height']);
  223.   $result = image_gd_close($res, $destination, $info['extension']);
  224.  
  225.   imageDestroy($res);
  226.   imageDestroy($im);
  227.  
  228.   return $result;
  229. }
  230.  
  231. /**
  232.  * Rotate an image the given number of degrees.
  233.  */
  234. function image_gd_rotate($source, $destination, $degrees, $bg_color = 0) {
  235.   if (!function_exists('imageRotate')) {
  236.     return false;
  237.   }
  238.  
  239.   $info = image_get_info($source);
  240.   if (!$info) {
  241.     return false;
  242.   }
  243.  
  244.   $im = image_gd_open($source, $info['extension']);
  245.   if (!$im) {
  246.     return false;
  247.   }
  248.  
  249.   $res = imageRotate($im, $degrees, $bg_color);
  250.   $result = image_gd_close($res, $destination, $info['extension']);
  251.  
  252.   return $result;
  253. }
  254.  
  255. /**
  256.  * Crop an image using the GD toolkit.
  257.  */
  258. function image_gd_crop($source, $destination, $x, $y, $width, $height) {
  259.   $info = image_get_info($source);
  260.   if (!$info) {
  261.     return false;
  262.   }
  263.  
  264.   $im = image_gd_open($source, $info['extension']);
  265.   $res = imageCreateTrueColor($width, $height);
  266.   imageCopy($im, $res, 0, 0, $x, $y, $width, $height);
  267.   $result = image_gd_close($res, $destination, $info['extension']);
  268.  
  269.   imageDestroy($res);
  270.   imageDestroy($im);
  271.  
  272.   return $result;
  273. }
  274.  
  275. /**
  276.  * GD helper function to create an image resource from a file.
  277.  */
  278. function image_gd_open($file, $extension) {
  279.   $extension = str_replace('jpg', 'jpeg', $extension);
  280.   $open_func = 'imageCreateFrom'. $extension;
  281.   if (!function_exists($open_func)) {
  282.     return false;
  283.   }
  284.   return $open_func($file);
  285. }
  286.  
  287. /**
  288.  * GD helper to write an image resource to a destination file.
  289.  */
  290. function image_gd_close($res, $destination, $extension) {
  291.   $extension = str_replace('jpg', 'jpeg', $extension);
  292.   $close_func = 'image'. $extension;
  293.   if (!function_exists($close_func)) {
  294.     return false;
  295.   }
  296.   return $close_func($res, $destination);
  297. }
  298.  
  299. ?>
  300.